home *** CD-ROM | disk | FTP | other *** search
/ Sound Fx / Sound Fx.iso / Software / UNZIPED / DWSTK / PLAYDWM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-10  |  6.5 KB  |  255 lines

  1. /******************************************************************************
  2. File:          playdwm.c
  3. Version:     2.22
  4. Tab stops: every 2 columns
  5. Project:     DWM Player
  6. Copyright: 1994-1995 DiamondWare, Ltd.    All rights reserved.
  7. Written:     Keith Weiner & Erik Lorenzen
  8. Purpose:     Contains simple example code to show how to load/play a .DWM file
  9. History:     94/10/21 KW Started
  10.                      95/02/21 EL Finalized
  11.                      95/03/22 EL Finalized for 1.01
  12.                      95/04/11 EL Finalized for 1.02
  13.                      95/06/16 EL Finalized for 1.03
  14.                      95/06/16 EL Finalized for 2.00, #ifdef'd _far for protected-mode
  15.                      95/10/05 EL Finalized for 2.10, added modual support for err_
  16.                      95/10/18 EL Finalized for 2.20, changed vol's to 95%
  17.                      95/12/07 EL Finalized for 2.21, no changes
  18.                      96/10/10 EL Finalized for 2.22, no changes
  19.  
  20. Notes
  21. -----
  22. This code isn't really robust when it comes to standard error checking
  23. and particularly recovery, software engineering technique, etc.  A buffer
  24. is statically allocated.    A better technique would be to use fstat() or stat()
  25. to determine the file's size then malloc(size).  The STK will handle songs
  26. larger than 64K (but not digitized sounds).  Obviously, you'd need to fread()
  27. such a file in chunks, or write some sort of hfread() (huge fread).  Also,
  28. exitting and cleanup is not handled robustly in this code.    The code below can
  29. only be validated by extremely careful scrutiny to make sure each case is
  30. handled properly.  A better method would the use of C's atexit function.
  31.  
  32. But all such code would make this example file less clear; its purpose was
  33. to illustrate how to call the STK, not how to write QA-proof software.
  34. ******************************************************************************/
  35.  
  36.  
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <conio.h>
  41.  
  42. #include "dws.h"
  43. #include "dwt.h"
  44. #include "err.h"
  45.  
  46.  
  47.  
  48. #define BUFFSIZE 65535        /* If the linker outputs the error */
  49.                                                     /* "stack plus data exceed 64K" */
  50.                                                     /* try reducing BUFFSIZE to about 32K, */
  51.                                                     /* or compile for large model. */
  52. #ifdef __FLAT__
  53.     byte song[BUFFSIZE];        /* _far isn't need for p-mode */
  54. #else
  55.     byte _far song[BUFFSIZE]; /* in r-mode the STK expects a _far ptr */
  56. #endif
  57.  
  58.  
  59.  
  60.  
  61. void main(int argc, char **argv)
  62. {
  63.     FILE                                *fp;
  64.     dws_DETECTOVERRIDES dov;
  65.     dws_DETECTRESULTS     dres;
  66.     dws_IDEAL                     ideal;
  67.     dws_MPLAY                     mplay;
  68.     int                                 input=0;
  69.     word                                musvol=255;     /* Default mxr volume at startup is max */
  70.     word                                songstatus;
  71.  
  72.     printf("\nPLAYDWM 2.22 is Copyright 1994-95 DiamondWare, Ltd.\n");
  73.     printf("All rights reserved.\n\n\n");
  74.  
  75.     if (argc < 2)
  76.     {
  77.         printf("Usage PLAYDWM <dwm-file>\n");
  78.         exit(-1);
  79.     }
  80.  
  81.     fp = fopen(argv[1], "rb");
  82.  
  83.     if (fp == NULL)
  84.     {
  85.         printf("Unable to open %s\n", argv[1]);
  86.         exit(-1);
  87.     }
  88.  
  89.     fread(song, (size_t)BUFFSIZE, 1, fp);  /* if filelen<BUFFSIZE, this works */
  90.  
  91.     fclose(fp);
  92.  
  93.     /*
  94.      . We need to set every field to -1 in dws_DETECTOVERRIDES struct;
  95.      . this tells the STK to autodetect everything.  Any other value
  96.      . overrides the autodetect routine, and will be accepted on
  97.      . faith, though the STK will verify it if possible.
  98.     */
  99.     dov.baseport = (word)-1;
  100.     dov.digdma     = (word)-1;
  101.     dov.digirq     = (word)-1;
  102.  
  103.     if (!dws_DetectHardWare(&dov, &dres))
  104.     {
  105.         err_Display(dws_ErrNo(), err_DWS);
  106.         exit(-1);
  107.     }
  108.  
  109.     if (!(dres.capability & dws_capability_FM))
  110.     {
  111.         printf("FM support not found\n");
  112.         exit(-1);
  113.     }
  114.  
  115.     /*
  116.      . The "ideal" struct tells the STK how you'd like it to initialize the
  117.      . sound hardware.    In all cases, if the hardware won't support your
  118.      . request, the STK will go as close as possible.  For example, not all
  119.      . sound boards will support all sampling rates (some only support five or
  120.      . six discrete rates).
  121.     */
  122.     ideal.musictyp     = 1;         /* for now, it's OPL2 music */
  123.     ideal.digtyp         = 0;         /* 0=No Dig, 8=8bit, 16=16bit */
  124.     ideal.digrate      = 0;         /* sampling rate, in Hz */
  125.     ideal.dignvoices = 0;         /* number of voices (up to 16) */
  126.     ideal.dignchan     = 0;         /* 1=mono, 2=stereo */
  127.  
  128.     if (!dws_Init(&dres, &ideal))
  129.     {
  130.         err_Display(dws_ErrNo(), err_DWS);
  131.         exit(-1);
  132.     }
  133.  
  134.     /*
  135.      . 72.8Hz is a decent compromise.  It will work in a Windows DOS box
  136.      . without any problems, and yet it allows music to sound pretty good.
  137.      . In my opinion, there's no reason to go lower than 72.8 (unless you
  138.      . don't want the hardware timer reprogrammed)--music sounds kinda chunky
  139.      . at lower rates.    You can go to 145.6 Hz, and get smoother (very
  140.      . subtly) sounding music, at the cost that it will NOT run at the correct
  141.      . (or constant) speed in a Windows DOS box.
  142.     */
  143.     dwt_Init(dwt_72_8HZ);
  144.  
  145.     /* Set Music Volume to about 95% max */
  146.     musvol = 242;
  147.  
  148.     if (!dws_XMusic(musvol))
  149.     {
  150.         err_Display(dws_ErrNo(), err_DWS);
  151.     }
  152.  
  153.     mplay.track = song;
  154.     mplay.count = 1;                    /* 0=infinite loop, 1-N num times to play sound */
  155.  
  156.     if (!dws_MPlay(&mplay))
  157.     {
  158.         err_Display(dws_ErrNo(), err_DWS);
  159.         goto KillIt;
  160.     }
  161.  
  162.     /*
  163.      . We're playing.  Until the song is over, let's allow the user
  164.      . to fiddle with the volume level (mixer) in the meantime
  165.     */
  166.  
  167.     if (!dws_MSongStatus(&songstatus))
  168.     {
  169.         err_Display(dws_ErrNo(), err_DWS);
  170.         goto KillIt;
  171.     }
  172.  
  173.     printf("Press + or - to change playback volume \n");
  174.  
  175.     while (songstatus)
  176.     {
  177.         if (kbhit())
  178.         {
  179.             input = getch();
  180.         }
  181.         else
  182.         {
  183.             input = 0;
  184.         }
  185.  
  186.         switch (input)
  187.         {
  188.             case 'q':
  189.             case 'Q':
  190.             case 27:                                /* ESC */
  191.             {
  192.                 if (!dws_MClear())
  193.                 {
  194.                     err_Display(dws_ErrNo(), err_DWS);
  195.                 }
  196.  
  197.                 break;
  198.             }
  199.             case '+':
  200.             {
  201.                 musvol++;
  202.  
  203.                 printf("Music Volume is %d\n", musvol);
  204.  
  205.                 if (!dws_XMusic(musvol))
  206.                 {
  207.                     err_Display(dws_ErrNo(), err_DWS);
  208.                 }
  209.  
  210.                 break;
  211.             }
  212.             case '-':
  213.             {
  214.                 musvol--;
  215.  
  216.                 printf("Music Volume is %d\n", musvol);
  217.  
  218.                 if (!dws_XMusic(musvol))
  219.                 {
  220.                     err_Display(dws_ErrNo(), err_DWS);
  221.                 }
  222.  
  223.                 break;
  224.             }
  225.         }
  226.  
  227.         if (!dws_MSongStatus(&songstatus))
  228.         {
  229.             err_Display(dws_ErrNo(), err_DWS);
  230.             goto KillIt;
  231.         }
  232.     }
  233.  
  234.     KillIt:
  235.  
  236.     /* If dwt is not inited calling dwt_Kill will have no effect */
  237.     dwt_Kill();
  238.  
  239.     if (!dws_Kill())
  240.     {
  241.         /*
  242.          . If an error occurs here, it's either dws_Kill_CANTUNHOOKISR
  243.          . or dws_NOTINITTED.  If it's dws_Kill_CANTUNHOOKISR the user
  244.          . must remove his tsr, and dws_Kill must be called again.    If it's
  245.          . dws_NOTINITTED, there's nothing to worry about at this point.
  246.         */
  247.         err_Display(dws_ErrNo(), err_DWS);
  248.  
  249.         if (dws_ErrNo() ==    dws_Kill_CANTUNHOOKISR)
  250.         {
  251.             goto KillIt;
  252.         }
  253.     }
  254. }
  255.